package com.finalcial.util;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Logger;

/**
 * 生成唯一的时间id
 * @author Administrator
 *
 */
public class TimeIdUtil {
	
	
	public TimeIdUtil() {
	}

	private static final Logger logger = Logger.getLogger("log");
	private static final FieldPosition HELPER_POSITION = new FieldPosition(0);
	private final static Format dateFormat = new SimpleDateFormat("YYYYMMddHHmmssS");
	private final static NumberFormat numberFormat = new DecimalFormat("0000");
	private static int seq = 0;
	private static final int MAX = 9999;
	
	/**
	 * 时间格式生成序列
	 * @return
	 */
	public static synchronized String getId() {
		 Calendar rightNow = Calendar.getInstance();
		 StringBuffer sb = new StringBuffer();
		 dateFormat.format(rightNow.getTime(), sb,HELPER_POSITION);
		 numberFormat.format(seq, sb, HELPER_POSITION);
		 if (seq == MAX) {
		 seq = 0;
		}else {
		 seq++;
		 }
		 
		 logger.info("THE SQUENCE IS :" +sb.toString());
		 
		 return sb.toString();
		 }
		
	
}
